package com.yuri.news.controller;

import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yuri.news.domain.Result;
import com.yuri.news.entity.Button;
import com.yuri.news.service.ButtonService;

/**
 * 按钮控制器
 * 
 * @author Yuri
 *
 */
// 允许跨域请求
@CrossOrigin
// 以json形式返回
@ResponseBody
// 控制器
@Controller
public class ButtonController {

	private final Logger logger = LoggerFactory.getLogger(ButtonController.class);
	@Autowired
	ButtonService buttonService;
	/**
	 *  添加按钮
	 * 
	 * @param button
	 * @return
	 */
	@RequestMapping(value = "addButton", method = RequestMethod.POST)
	public Result<Button> addButton(Button button) {
		logger.info("\n addButton Button = " + button.toString());
		Result<Button> result = new Result<Button>();
		try {
			Button btn = buttonService.saveButton(button);
			if(btn != null) {
				result.setSuccess("添加成功！");
			}else {
				result.setError("添加失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		return result;
	}

	/**
	 * 查询所有按钮信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "findButtonAll", method = RequestMethod.GET)
	public Result<Button> findButtonAll() {
		logger.info("\n 查询所有按钮（findButtonAll）");
		Result<Button> result = new Result<>();
		
		try {
			List<Button> buttons = buttonService.listButton();
			if(buttons != null) {
				result.setData(buttons);
				result.setSuccess("查询成功！");
			}else {
				result.setError("暂无数据！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 查询所有按钮（findButtonAll） result = " + result);
		return result;
	}

	/**
	 * 根据id查询按钮信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "findButtonById", method = RequestMethod.GET)
	public Result<Button> findButtonById(Long id) {
		logger.info("\n 根据id查询所有按钮信息（findButtonById）开始 id = " + id);
		Result<Button> result = new Result<>();
		try {
			List<Button> buttons = new ArrayList<>();
			Button button = buttonService.getButtonById(id);
			
			if(button != null) {
				buttons.add(button);
				
				result.setData(buttons);
				result.setSuccess("查询成功！");
			}else {
				result.setError("无此按钮信息！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 根据id查询所有按钮信息（findButtonById）结束 result = " + result);
		return result;
	}

	/**
	 * 根据id删除按钮
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "delButtonById", method = RequestMethod.DELETE)
	public Result<Button> delButtonById(Long id) {
		logger.info("\n 根据id删除按钮（delButtonById）开始 id = " + id);
		Result<Button> result = new Result<Button>();
		try {
			if(buttonService.deleteButton(id)) {
				result.setSuccess("删除成功！");
			}else {
				result.setError("无此按钮信息！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误!");
		}
		logger.info("\n 根据id删除按钮（delButtonById）结束 result = " + result);
		return result;
	}
	@RequestMapping(value = "updateButton", method = RequestMethod.PUT)
	public Result<Button> updateButton(Button button) {
		logger.info("\n 更新按钮信息（updateButton）开始 button="+button);
		Result<Button> result = new Result<>();
		try {
			if(buttonService.updateButton(button)) {
				result.setSuccess("更新成功！");
			}else {
				result.setError("无此按钮信息！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 更新按钮信息（updateButton）结束 result="+result);
		return result;
	}
}
